﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LowLevelGraphics.Filter;

namespace LowLevelGraphics
{
    public class Resampler
    {
        public UnsafeBitmap Resample(UnsafeBitmap imageOrigSize, int nCount, bool _bResample, IIteratedFilter[] _aFilter)
        {
            UnsafeBitmap imageSubSample = (UnsafeBitmap)imageOrigSize.Clone();
            if (_bResample)
            {
                for (int i = 0; i < nCount; i++)
                {
                    int nWidth = imageSubSample.Width / 2;
                    int nHeight = imageSubSample.Height / 2;
                    if (nWidth < 1 || nHeight < 1) break;
                    imageSubSample = new UnsafeBitmap(imageSubSample, new Size(nWidth, nHeight));
                    foreach (IIteratedFilter filter in _aFilter)
                    {
                        filter.Execute(imageSubSample);
                    }
                }

            }
            else
            {
                int nWidth = (int)(imageOrigSize.Width / Math.Pow(2, nCount));
                int nHeight = (int)(imageOrigSize.Height / Math.Pow(2, nCount));
                if (nWidth < 1) nWidth = 1;
                if (nHeight < 1) nHeight = 1;
                imageSubSample = new UnsafeBitmap(imageSubSample, new Size(nWidth, nHeight));
            }
            imageSubSample = new UnsafeBitmap(imageSubSample, new Size(imageOrigSize.Width, imageOrigSize.Height));
            return imageSubSample;
        }
    }
}
